﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ArchiveBorrow.Models;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveBorrow.Services;
using DreamBuilder.Core.Container;
using Web.SystemConfig.Services;
using System.Data;
using Web.SystemConfig.WorkFlow;

namespace Web.Views.ArchiveBorrow.ArchiveBorrowApply
{
    public partial class ArchiveBorrowFaildView : PageBase
    {
        #region 私有属性
        private ArchiveBorrowModel.ARCHIVEBORROWRow m_CurrentArchiveBorrow = null;
        private Web.SystemLogin.Models.User m_CurrentUser
        {
            get
            {
                return (Web.SystemLogin.Models.User)Session["CurrentUser"];
            }
        }

        [Dependency]
        public IArchiveBorrowService m_IArchiveBorrowService
        {
            get;
            set;
        }
        #endregion

        public string Referer = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.GetArchiveBorrowDetail();
            if (!IsPostBack)
            {
                var referer = Request.Headers["Referer"];
                if (!string.IsNullOrEmpty(referer))
                {
                    this.Referer = referer;
                }
                else
                {
                    this.Referer = Request.Url.OriginalString;
                }

                this.BindArchiveBorrowDetail();
                this.BindOperateLog();
            }
        }

        #region 私有方法
        /// <summary>
        /// 初始化档案借阅对象
        /// </summary>
        private void GetArchiveBorrowDetail()
        {
            if (string.IsNullOrEmpty(Request.QueryString["DataID"]))
            {
                throw new Exception("参数DataID不能为空！");
            }
            Guid dataID = new Guid(Request.QueryString["DataID"]);
            ArchiveBorrowModel model = this.m_IArchiveBorrowService.GetSingleArchiveBorrowModelByDataID(dataID);
            if (model != null && model.ARCHIVEBORROW.Count > 0)
            {
                this.m_CurrentArchiveBorrow = model.ARCHIVEBORROW[0];
            }
        }

        /// <summary>
        /// 绑定档案借阅对象
        /// </summary>
        private void BindArchiveBorrowDetail()
        {
            if (this.m_CurrentArchiveBorrow != null)
            {
                this.txtBorrowTime.Text = m_CurrentArchiveBorrow.CREATETIME.ToShortDateString();
                string userID = m_CurrentArchiveBorrow.CREATEUSERID;
                this.txtBorrowUser.Text = m_CurrentArchiveBorrow.BORROWUSERNAME;
                if (!m_CurrentArchiveBorrow.IsUSERORGNAMENull())
                {
                    this.txtBorrowUserOrg.Text = m_CurrentArchiveBorrow.USERORGNAME;
                }
                if (!m_CurrentArchiveBorrow.IsVALIDPERIODNull())
                {
                    this.txtValidPeriod.Text = m_CurrentArchiveBorrow.VALIDPERIOD.ToShortDateString();
                }
                if (!m_CurrentArchiveBorrow.IsCANDOWNLOADNull())
                {
                    this.cboCanDownload.Checked = m_CurrentArchiveBorrow.CANDOWNLOAD == 1 ? true : false;
                }
                if (!m_CurrentArchiveBorrow.IsBORROWREASONNull())
                {
                    this.txtReason.Text = m_CurrentArchiveBorrow.BORROWREASON;
                }

                //外厂借阅，隐藏下载和浏览
                if (m_CurrentArchiveBorrow.BOROWTYPE != null && m_CurrentArchiveBorrow.BOROWTYPE == 1)
                {
                    if (!m_CurrentArchiveBorrow.IsCANVIEWNull())
                    {
                        this.cboCanView.Checked = m_CurrentArchiveBorrow.CANVIEW == 1 ? true : false;
                    }
                    if (!m_CurrentArchiveBorrow.IsVALIDPERIODNull())
                    {
                        this.txtValidPeriod.Text = m_CurrentArchiveBorrow.VALIDPERIOD.ToShortDateString();
                    }
                }
                else
                {
                    //隐藏下载
                    this.trDown.Visible = false;
                    //隐藏本部室审批意见
                    this.trBBSYJ.Visible = false;
                }

                //绑定新加字段 2012-4-13
                //绑定档案类型
                if (!string.IsNullOrEmpty(m_CurrentArchiveBorrow.ARCHIVETYPE))
                {
                    if (m_CurrentArchiveBorrow.USERTYPE.IndexOf("1") != -1)
                    {
                        this.cblARCHIVETYPE.Items[0].Selected = true;
                    }
                    if (m_CurrentArchiveBorrow.USERTYPE.IndexOf("2") != -1)
                    {
                        this.cblARCHIVETYPE.Items[1].Selected = true;
                    }
                    if (m_CurrentArchiveBorrow.USERTYPE.IndexOf("3") != -1)
                    {
                        this.cblARCHIVETYPE.Items[2].Selected = true;
                    }
                }
                //绑定利用方式
                if (!string.IsNullOrEmpty(m_CurrentArchiveBorrow.USERTYPE))
                {
                    if (m_CurrentArchiveBorrow.USERTYPE.IndexOf("1") != -1)
                    {
                        this.cblUSERTYPE.Items[0].Selected = true;
                    }
                    if (m_CurrentArchiveBorrow.USERTYPE.IndexOf("2") != -1)
                    {
                        this.cblUSERTYPE.Items[1].Selected = true;
                    }
                    if (m_CurrentArchiveBorrow.USERTYPE.IndexOf("3") != -1)
                    {
                        this.cblUSERTYPE.Items[2].Selected = true;
                        this.trCopyPage.Visible = true;
                        if (!string.IsNullOrEmpty(m_CurrentArchiveBorrow.COPYPAGE))
                        {
                            this.txtCopyPage.Text = m_CurrentArchiveBorrow.COPYPAGE;
                        }
                    }
                    else
                    {
                        this.trCopyPage.Visible = false;
                    }
                }
                else
                {
                    this.trCopyPage.Visible = false;
                }
                //绑定身份证，电话
                if (!string.IsNullOrEmpty(m_CurrentArchiveBorrow.IDCARD))
                {
                    this.txtIDCARD.Text = m_CurrentArchiveBorrow.IDCARD;
                }
                if (!string.IsNullOrEmpty(m_CurrentArchiveBorrow.TELNUM))
                {
                    this.txtTELNUM.Text = m_CurrentArchiveBorrow.TELNUM;
                }

                this.SetArchiveType();
            }
        }

        private void SetArchiveType()
        {
            string archiveTypeString = string.Empty;
            switch ((int)this.m_CurrentArchiveBorrow.ARCHIVETYPEID)
            {
                case 1:
                    archiveTypeString = "文书类";
                    break;
                case 2:
                    archiveTypeString = "产品类";
                    break;
                case 3:
                    archiveTypeString = "科学技术研究类";
                    break;
                case 4:
                    archiveTypeString = "建设项目类";
                    break;
                case 5:
                    archiveTypeString = "设备仪器类";
                    break;
                case 6:
                    archiveTypeString = "会计类";
                    break;
                case 7:
                    archiveTypeString = "油气勘探类";
                    break;
                case 8:
                    archiveTypeString = "音像类";
                    break;
                case 9:
                    archiveTypeString = "实物类";
                    break;
                default:
                    archiveTypeString = "文书类";
                    break;
            }

            this.txtArchiveType.Text = archiveTypeString;
        }

        /// <summary>
        /// 绑定操作日志
        /// </summary>
        private void BindOperateLog()
        {
            if (this.m_CurrentArchiveBorrow != null)
            {
                DataTable logDt = this.GetWorkFlowLog(this.m_CurrentArchiveBorrow.DATAID);
                if (logDt == null) return;
                DataRow[] BBSLDCommentRows = logDt.Select("OperateID in (1001,1002) and FromStatusID=501");//本部室领导意见
                DataRow[] XXZXDAYCommentRows = logDt.Select("OperateID in (1003,1004,1005) and FromStatusID=502");//档案员审批意见
                DataRow[] DAXCBMLDComnentRows = logDt.Select("OperateID in (1006,1007) and FromStatusID=503");//档案形成部门审批意见
                DataRow[] XXZXLDYJCommentRows = logDt.Select("OperateID in (1008,1009) and FromStatusID=504");//信息中心领导审批意见
                DataRow[] XXZXDAYCommentRowsWC = logDt.Select("OperateID =1011 and FromStatusID=506");//档案员发起借阅，档案员审批意见
                DataRow[] DAXCBMLDComnentRowsWC = logDt.Select("OperateID in (1012,1013) and FromStatusID=507");//档案员发起借阅，档案形成部门审批意见

                //绑定日志
                this.BindCommentData(this.txtMessageBBSLD, BBSLDCommentRows);//绑定本部室领导审批意见
                //this.BindCommentData(this.txtMessageDAY, XXZXDAYCommentRows);//档案员审批意见
                this.BindCommentData(this.txtMessageDAXCBMLD, DAXCBMLDComnentRows);//档案形成部门审批意见
                this.BindCommentData(this.txtMessageXXZXLD, XXZXLDYJCommentRows);//信息中心领导审批意见
                if (this.m_CurrentArchiveBorrow.BOROWTYPE == 2)
                {
                    //this.BindCommentData(this.txtMessageDAY, XXZXDAYCommentRowsWC);//档案员发起借阅，档案员审批意见
                    this.BindCommentData(this.txtMessageDAXCBMLD, DAXCBMLDComnentRowsWC);//档案形成部门审批意见
                }
            }
        }

        private void BindCommentData(TextBox txt, DataRow[] rows)
        {
            if (rows != null && rows.Length > 0)
            {
                if (rows[0]["commentmessage"] != null && rows[0]["commentmessage"] != DBNull.Value)
                {
                    txt.Text = rows[0]["commentmessage"].ToString();
                }
                else
                {
                    txt.Text = "系统：无审批意见";
                }
            }
        }

        /// <summary>
        /// 获取操作日志
        /// </summary>
        /// <param name="dataID"></param>
        /// <returns></returns>
        private DataTable GetWorkFlowLog(string dataID)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            return oWorkFlow.GetOperateLogList(ArchiveTableEnum.ArchiveBorrow, dataID);
        }
        #endregion
    }
}